/*
* @Author: z.p
* @Project: Zdotp
* @Date: 2025年01月02日
* @Description: 新增线程模型, 取消了actor和thread的一一映射
*/

namespace Zdotp.Actor.Group;

public class ZGroup
{
    public int GroupId;
    private float _deltaTime;
    private bool _isRunning;

    public ZGroup(int groupId, float deltaTime)
    {
        GroupId = groupId;
        _deltaTime = deltaTime;
    }
    
    private readonly List<ActorBase> _actors = new();
    public void AddActor(ActorBase actor)
    {
        _actors.Add(actor);
    }

    public void Start()
    {
        _isRunning = true;
        Task.Run(() =>
        {
            foreach (var actor in _actors) actor.Start();
            while (_isRunning)
            {
                foreach (var actor in _actors) actor.Update(_deltaTime);
                Thread.Sleep(TimeSpan.FromSeconds(_deltaTime));
            }
            foreach (var actor in _actors) actor.Destroy();
            _actors.Clear();
        });
    }

    public void Destroy()
    {
        _isRunning = false;
    }
}